package com.qcby.controller;


import com.qcby.Constants;
import com.qcby.model.ScreeningOrganize;
import com.qcby.model.dto.*;
import com.qcby.model.resp.RespEntity;
import com.qcby.query.ScreeningOrganizeQuery;
import com.qcby.service.HallService;
import com.qcby.service.ScreeningOrganizeService;
import com.qcby.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/organize")
public class ScreeningOrganizeController {
    @Autowired
    private ScreeningOrganizeService service;
    @Autowired
    private HallService hallService;

    @RequestMapping("/listAll")
    public RespEntity<List<ScreeningOrganizeVo>> listAll() {
        return null;
    }

    //查询场次信息——前台
    @RequestMapping("/selectCondition")
    public RespEntity<Page<OrganizeVo>> searchOrganize(@RequestBody ScreeningOrganizeQueryDto queryDto) {
        // 校验字段合法性
        queryDto.isParamValid();

        if (queryDto.getPageNo() == null) {
            queryDto.setPageNo(1);
        }
        Page<OrganizeVo> page = service.selectCondition(queryDto);
        System.out.println(page);
        System.out.println(queryDto);
        return RespEntity.successCustomize(Constants.FLAG_200, "查询场次成功！", page);
    }

    //查询场次信息——后台
    @RequestMapping("/selectCondition2")
    public RespEntity<Page<OrganizeVo>> searchOrganize2(@RequestBody ScreeningOrganizeQueryDto2 queryDto2) {
        if (queryDto2.getPageNo() == null) {
            queryDto2.setPageNo(1);
        }
        Page<OrganizeVo> page = service.selectCondition2(queryDto2);
        System.out.println(page);
        System.out.println(queryDto2);
        return RespEntity.successCustomize(Constants.FLAG_200, "查询场次成功！", page);
    }

    //查询场次信息
    @RequestMapping("/searchOrganize")
    public RespEntity<ScreeningOrganizeVo> searchOrganize(ScreeningOrganizeQuery sq) {
        if (sq != null) {
            if (sq.getPageNo() == null) {
                sq.setPageNo(1);
                Page<ScreeningOrganize> page = service.selectObjectByCondition(sq, "0");
                System.out.println(page);
                System.out.println(sq);
                return RespEntity.successCustomize(Constants.FLAG_200, "", new ScreeningOrganizeVo(page, sq));
            }

        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "", null);
    }

    //删除场次
    @RequestMapping("/deleteOrganize")
    public RespEntity<String> deleteOrganize(@RequestParam Integer soId) {
        if (soId != null) {
            service.deleteByPrimaryKey(soId);
            return RespEntity.successCustomize(Constants.FLAG_200, "", "删除成功");
        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "", "删除失败，参数不能为空");
    }

    //添加场次信息
    //需要查询所有的电影信息， 影院信息， 放映厅信息
    @RequestMapping("/addOrganize")
    public RespEntity<String> addOrganize(@RequestBody ScreeningOrganizeCreateDto soDto) {
        // TODO 校验新插入的影厅的场次起止时间是否与现有影厅场次起止时间存在交叉
        if (!soDto.isValid()) {
            ScreeningOrganize so = new ScreeningOrganize();
            Integer hid = soDto.getHId();
            so.setHId(hid);
            so.setMid(soDto.getMid());
            so.setBeginTime(soDto.getBeginTime());
            so.setEndTime(soDto.getEndTime());
            so.setPrice(soDto.getPrice());
            Integer leftSet = hallService.selectByPrimaryKey(hid).gethCapacity();
            so.setLeftSeat(leftSet);
            service.insert(so);
            return RespEntity.successCustomize(Constants.FLAG_200, "添加场次成功！", null);
        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "添加场次失败，字段存在空值或者不合法", null);
    }

    //根据ID进行回显数据
    @RequestMapping("/getOrganizeById")
    public RespEntity<OrganizeVo> getOrganizeById(@RequestParam Integer soId) {
        if (soId != null) {
            OrganizeVo organizeVo = service.selectById(soId);
            System.out.println(organizeVo);
            return RespEntity.successCustomize(Constants.FLAG_200, "查询成功", organizeVo);
        }
        return RespEntity.errorNoData(Constants.FLAG_400, "查询失败，参数不能为空!");
    }

    //修改操作
    @RequestMapping("/updateOrganize")
    public RespEntity<String> updateOrganize(@RequestBody ScreeningOrganize organizeNew) {
        if (organizeNew != null) {
            if (organizeNew.getSoId() != null) {
                service.updateByPrimaryKeySelective(organizeNew);
                return RespEntity.successCustomize(Constants.FLAG_200, "", "修改成功");
            }
        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "", "修改失败");
    }
}